<?php
require_once 'ApibaseController.php';

/**
 * ASメンバーズネット問い合わせAPI<br />
 *
 * ASメンバーズネット問い合わせAPI
 *
 * @package apamanshop
 * @author ushijima
 * @copyright コピーライト(要確認)
 */
class Inquiry_ApiasmemberController extends Inquiry_ApibaseController
{
	/**
	 * ASメンバーズネット問合せAPIアクション
	 *
	 * @access public
	 * @return void
	 */
	public function indexAction()
	{

		// 変数初期化処理 start

		$strSelID = $this->_params['BukkenCd'];
		$strUseComName = $this->_params['UserComName'];
		$strNameSei = $this->_params['NameSei'];
		$strNameMei = $this->_params['NameMei'];
		$strCntctWayMail = $this->_params['CntctWayMail'];
		$strCntctWayTel = $this->_params['CntctWayTel'];
		$strCntctWayFax = $this->_params['CntctWayFax'];
		$strMail = $this->_params['Mail'];
		$strMailMgznFlg = $this->_params['MailMgznFlg'];
		$strTel = $this->_params['Tel'];
		$strNeedTimeKbn = $this->_params['NeedTimeKbn'];
		$strFax = $this->_params['Fax'];
		$strInqryContents = $this->_params['Comment'];
		$strComId = $this->_params['ComId'];

		//送信時刻
		$strTimeStr = $this->_params['TimeStr'];

		//--- メール判定 ---
		$this->setSendMailFlag($this->_params['Mail']);

		// 希望時間
		$arrNeedTimeKbn = Array("希望なし","10時以前","10時～12時","12時～14時","14時～16時","16時～18時","18時以降");

		$add_params['arrNeedTimeKbn'] = $arrNeedTimeKbn[$strNeedTimeKbn];

		// 希望連絡手段(表示用)
		$strCntctWayStr = "";
		// 希望連絡(メールが設定されている場合)
		if ($cntctWayMail == "1")  $strCntctWayStr = $strCntctWayStr . "メール,";
		// 希望連絡(TEL)が設定されている場合
		if ($cntctWayTel == "1") $strCntctWayStr = $strCntctWayStr . "ＴＥＬ,";
		// 希望連絡(FAX)が設定されている場合
		if ($cntctWayFax == "1") $strCntctWayStr = $strCntctWayStr . "ＦＡＸ,";

		// 希望連絡手段に値が入ってたら、最後のカンマを除去する
		if($strCntctWayStr != "") $strCntctWayStr = rtrim($strCntctWayStr,",");

		$this->_params['CntctWayStr'] = $strCntctWayStr;

		// 希望連絡手段CD
		$strCntctWayCd = "";
		// 希望連絡(TEL)が設定されている場合
		if ($cntctWayTel == "1") {
			$strCntctWayCd = strCntctWayCd & "1";
		} else {
			$strCntctWayCd = strCntctWayCd & "0";
		}
		// 希望連絡(メールが設定されている場合)
		if ($cntctWayMail == "1") {
			$strCntctWayCd = strCntctWayCd & "1";
		} else {
			$strCntctWayCd = strCntctWayCd & "0";
		}

		// 希望連絡(FAXが設定されている場合)
		if ($cntctWayFax == "1") {
			$strCntctWayCd = strCntctWayCd & "1";
		} else {
			$strCntctWayCd = strCntctWayCd & "0";
		}

		$this->_params['CntctWayCd'] = $strCntctWayCd;
		$resApi = array();
		// ﾛｸﾞ情報格納処理 *****************************************
		try {
			// モデルの呼び出し
			$this->_model = new Inquiry_Model_Apiinquiry();

			//トランザクション開始
			$db = $this->_model->getDbConnection();

			//トランザクション開始
			$this->_model->startTransaction($db);

			//企業情報取得
			$Complist = $this->_model->getInqCompanyQry($this->_params);
			//問合わせID生成
			$strToiawaseId = strval($this->GetTimeString()) . strval($this->GetToiawaseSEQ());

			$add_params['InqNo'] = $strToiawaseId;
			$add_params['CompanyName'] = $Complist[0]['M86_COMPANY_NAME'];
			$add_params['ComGenruFlg'] = $Complist[0]['M86_GENRU_FLAG'];
			$add_params['intKoujyoRitsu'] = $Complist[0]['M86_SHATAKU_KOUJYORITSU'];
			$add_params['TensouMail'] = $Complist[0]['M86_TENSOU_MAIL'];
			if($Complist['M86_GENRU_FLAG'] != "0") {
				if ($this->isLogIns()) {
					//問合せﾕｰｻﾞ情報格納ｸｴﾘ取得処理
					array_push($resApi,$this->_model->insInqUserLog($db,$this->_params,$add_params));

				}
			}

			//物件リスト作成
			$Bukkenlist = $this->_model->GetInqASBukkenList($strSelID,$add_params);

			$add_params['CntB'] = count($Bukkenlist);

			//初回フラグ
			$intFlgFirst = 1;
			$intCntB = 0;

			foreach ($Bukkenlist as $value){
				$strShopCd = $value['T03_TENPO_CD'];

				if(($strShopCd != $strShopCdBk) && $intFlgFirst != 1) {
					$result =  $this->SendMailMain($strMail,$add_params);
					if($result == "0"){
						 $this->raiseError('メール送信エラー');
					}

					if($Complist[0]['M86_GENRU_FLAG'] != "0") {
						if ($this->isLogIns() == true) {
							//対応状況TBLﾃﾞｰﾀ挿入処理
							$add_params['ViewKbn'] = 3;	//部屋番号取得前のため設定なし
							$add_params['OrgHeyaCd'] = "";
							$this->_params['ShopCd'] = $strShopCdBk;
							array_push($resApi,$this->_model->pageViewLog($db,$this->_params,$add_params));
						}
					}

					//物件ｶｳﾝﾀ 初期化
					$intCntB = 0;
				}

				$add_params['strShopNm'] = $value['M05_TENPO_NAME'];
				$add_params['strShopMail'] = $value['M05_TENPO_MAIL'];
				$add_params['strShopTel'] = $value['M05_TENPO_TEL01'];
				$add_params['strInqNo'] = $value['T03_ORIGINAL_HEYA_CD01'];
				$add_params['strBuildNm'] = $value['T03_TATEMONO_NAME'];
				$add_params['strBuildCdOrg'] = $value['T03_ORIGINAL_TATEMONO_CD01'];
				$add_params['strAddress'] = $value['T03_JYUUSHO_TEXT'];
				$add_params['strKsdsKbn'] = $value['T03_KASHIDASHI_KUBUN'];
				$add_params['strPmCmpnyCd'] = $value['T03_KOUKOKU_KAISHA_CD'];
				$add_params['strPmInqNo'] = $value['T03_KOUKOKU_TOIAWASE_NO'];
				$add_params['strPrefCd'] = $value['T03_TODOUFUKEN_CD'];
				$add_params['strKoujyoRitsu'] = $value['SHATAKU_KOUJYORITSU'];

				//物件ｶｳﾝﾀのｲﾝｸﾘﾒﾝﾄ
				$intCntB = $intCntB + 1;
				if ($intFlgFirst == 1){
					$add_params['strInqNoComma'] = $add_params['strInqNo'];
				}else {
					$add_params['strInqNoComma'] = $add_params['strInqNoComma'] . "," . $add_params['strInqNo'];
				}
				if($Complist[0]['M86_GENRU_FLAG'] != "0") {
					if ($this->isLogIns() == true) {

						array_push($resApi, $this->_model->insToiawaseBukken($db,$this->_params,array_merge($add_params,$value)));

						//管理会社が設定されている場合
						if (($add_params['strPmCmpnyCd'] != "") && ($this->isAosShop($strShopCd) != 1)) {
							//--- 管理会社物件ﾛｸﾞ情報挿入処理 ---
							array_push($resApi,$this->_model->InsKanriBkknLog($db,$add_params));
						}
						//--- 問い合わせ店舗情報格納処理 ---
						//対応状況TBLﾃﾞｰﾀ挿入処理
						$add_params['ViewKbn'] = 2;	//部屋番号取得前のため設定なし
						$add_params['OrgHeyaCd'] = $add_params['strInqNo'];
						$this->_params['ShopCd'] = $strShopCd;
						array_push($resApi,$this->_model->pageViewLog($db,$this->_params,$add_params));
					}
				}

				$intFlgFirst = 0;				//初回ﾌﾗｸﾞ OFF
				$strShopCdBk = $strShopCd;		//店舗CD ﾊﾞｯﾌｧﾘﾝｸﾞ

			}
			// ﾒｰﾙ送信処理 ********************************************
			$result =  $this->SendMailMain($strMail,$add_params);

			if($result == "0"){
				 $this->raiseError('メール送信エラー');
			}

			//初回処理でない場合
			if ($intFlgFirst == 0) {
				if($Complist[0]['M86_GENRU_FLAG'] != "0") {
					if ($this->isLogIns() == true) {
						//対応状況TBLﾃﾞｰﾀ挿入処理
						$add_params['ViewKbn'] = 3;	//部屋番号取得前のため設定なし
						$add_params['OrgHeyaCd'] = "";
						$this->_params['ShopCd'] = $strShopCd;
						array_push($resApi,$this->_model->pageViewLog($db,$this->_params,$add_params));
					}
				}
			}

			__log_debug(__FILE__, __LINE__, __METHOD__, "コミットします");
			$this->_model->commitTransaction($db);
			$db->closeConnection();

			$result = 1;

		} catch (Exception $e) {
			__log_debug(__FILE__, __LINE__, __METHOD__, "ロールバックします");
			$this->_model->rollbackTransaction($db);
			$db->closeConnection();
			$result = 0;
		}

		// 返却データ作成
		$resResult = array('api_result' => array('result' => $result,'res_detail' => $resApi));
		// 返却処理
		$this->_api_data = $resResult;
		$this->display();

	}

	//メール送信制御
	private function SendMailMain($mail,$addinfo) {
		// ｽﾃｰﾀｽ初期化
		$userResult   = 1;
		$shopResult   = 1;
		$officeResult = 1;
		$houjinResult = 1;
		$mailResult   = 1;

		// ユーザへメール送信
		if($mail != ""){
			$userResult = $this->SendMailUser($mail,$addinfo);
		}

		// 本部へのメール送信
		if($addinfo['strShopMail'] != "") {
			$shopResult = $this->SendMailShop($mail,$addinfo);
			$officeResult = $this->SendMailOffice($mail,$addinfo);

		// メール転送処理追加により削除
		//	//ｼﾞｬﾝﾙﾌﾗｸﾞが39、40(ｺﾆｶﾐﾉﾙﾀｸﾞﾙｰﾌﾟ)であった場合
		//	if (($addinfo['ComGenruFlg'] == "39") || ($addinfo['ComGenruFlg'] == "40")) {
		//		if ($this->isLogIns() == true) {
		//			//--- ﾒｰﾙ送信処理 ---
		//			$houjinResult = $this->SendMailHoujin($mail, $addinfo);
		//		} else {
		//			//--- ﾒｰﾙ送信処理 ---
		//			$houjinResult = $this->SendMailHoujin($mail, $addinfo);
		//		}
		//	}
		} else{
			// 店舗のメールアドレスが設定されていない場合
			// ﾒｰﾙ送信処理(ﾒｰﾙｱﾄﾞﾚｽ未設定時)
			$mailResult = $this->SendMailNoMail($mail, $addinfo);
		}
		$result = 1;

		if ( $userResult == 0 || $shopResult == 0 ||  $officeResult == 0 || $houjinResult == 0 || $mailResult == 0) {
			$result = 0;
		}
		return $result;
	}

	// 問い合わせユーザへのメール送信
	private function SendMailUser($mail,$addinfo) {

		// from(送信元)、to（あて先)
		$strMailTo = array($mail => "");							//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailFrom = array(Apm_Config_Mail::MAIL_AS => "");		//送信元ﾒｰﾙｱﾄﾞﾚｽ
		$strMailReply = array(Apm_Config_Mail::MAIL_AS => "");
		// メール転送処理
		$strMailCc = array();
		if (!empty($addinfo['TensouMail'])) {
			$tensouMail = explode(",", $addinfo['TensouMail']);
			foreach ($tensouMail as $val) {
				$strMailCc[$val] = "";
			}
		}

		// メール本文に必要なパラメータ
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = "［アパマンショップ］" . $addinfo['CompanyName'] . "専用ページでの物件お問合わせ";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;
		if($addinfo['ComGenruFlg'] != "0") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_REPLY_TO_KEY] = $strMailReply;
		}
		if ($this->isSendUser()) {
			// 店舗ジャッジメールでなければCC追加 => ジャッジメールでもCC追加(2012/02/24update)
			// ※※DB登録されている転送先へメールが飛ぶので注意する事※※
			if(!empty($strMailCc)) {
				$mail_params[Apm_Otheritems::SEND_MAIL_EX_CC_KEY] = $strMailCc;
			}
			$result = $this->sendToiawaseMail("MailAsmemberUser.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}
		return $result;
	}

	// 店舗のメール送信
	private function SendMailShop($mail,$addinfo) {

		$strMailFrom = array(Apm_Config_Mail::MAIL_AS => "");		//送信元ﾒｰﾙｱﾄﾞﾚｽ
		$strMailTo =   array($addinfo['strShopMail'] => "");		//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailReply = array($mail => "");
		//@ 送信先ﾒｰﾙｱﾄﾞﾚｽ
		// 本文の設定
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = "［アパマンショップ］" . $addinfo['CompanyName'] . "専用ページからの物件お問合わせ";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_REPLY_TO_KEY] = $strMailReply;
		if ($this->isAosShop($this->_params['ShopCd']) == true) {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_HEADER_KEY] = $this->GetMailAosParam($addinfo['strInqNoComma']);
		}

		$addinfo['intFlg'] = "1";
		//メール制御ロジック
		if($this->isSendShop() == true) {
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailAsmember" . $this->_params['ComId'] . ".tpl",array_merge($this->_params,$addinfo), $mail_params);
		}else if($this->getReplaceShopMail() != "") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($this->getReplaceShopMail() => "");
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailAsmember" . $this->_params['ComId'] . ".tpl",array_merge($this->_params,$addinfo), $mail_params);
		}

		return $result;
	}

	// 本部のメール送信
	private function SendMailOffice($mail,$addinfo) {

		$strMailFrom = array(Apm_Config_Mail::MAIL_AS => "");		//送信元ﾒｰﾙｱﾄﾞﾚｽ
		$strMailTo = array(Apm_Config_Mail::MAIL_AS => "");			//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailBcc = array(Apm_Config_Mail::MAIL_NETWORK => "");

		//@ 送信先ﾒｰﾙｱﾄﾞﾚｽ
		// 本文の設定
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] =  "［アパマンショップ］" . $addinfo['CompanyName'] . "専用ページからの物件お問合わせ" . "(" . $addinfo['strShopNm'] . ")";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_BCC_KEY] = $strMailBcc;
		if ($this->isAosShop($this->_params['ShopCd']) == true) {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_HEADER_KEY] = $this->GetMailAosParam($addinfo['strInqNoComma']);
		}

		$addinfo['intFlg'] = "2";
		//メール制御ロジック
		if($this->isSendHonbu() == true) {
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailAsmember" . $this->_params['ComId'] . ".tpl",array_merge($this->_params,$addinfo), $mail_params);
		}else if($this->getReplaceHonbuMail() != "") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($this->getReplaceHonbuMail() => "");
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_BCC_KEY] = array($this->getReplaceHonbuMail() => "");
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailAsmember" . $this->_params['ComId'] . ".tpl",array_merge($this->_params,$addinfo), $mail_params);
		}

		return $result;
	}

	// 業者へのメール送信
	private function SendMailHoujin($mail,$addinfo) {

		$strSubject = "［アパマンショップ］" . $addinfo['CompanyName'] . "専用ページでの物件お問合わせ";
		$strMailFrom = array(Apm_Config_Mail::MAIL_AS => "");		//送信元ﾒｰﾙｱﾄﾞﾚｽ
		$strMailTo = array($addinfo['TensouMail'] => "");			//送信先ﾒｰﾙｱﾄﾞﾚｽ

		//@ 送信先ﾒｰﾙｱﾄﾞﾚｽ
		// 本文の設定
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = $strSubject;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		if($addinfo['ComGenruFlg'] != "0") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($mail => "");
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailAsmemberKigyo.tpl",array_merge($this->_params,$addinfo), $mail_params);
		} else {
			if($this->isSendHonbu() == true) {
				// ﾒｰﾙ送信処理
				$result = $this->sendToiawaseMail("MailAsmemberKigyo.tpl",array_merge($this->_params,$addinfo), $mail_params);
			}else if($this->getReplaceHonbuMail() != "") {
				$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($this->getReplaceHonbuMail() => "");
				// ﾒｰﾙ送信処理
				$result = $this->sendToiawaseMail("MailAsmemberKigyo.tpl",array_merge($this->_params,$addinfo), $mail_params);
			}
		}
		return $result;
	}

	// 問い合わせメールエラー
	private function SendMailNoMail($mail, $addinfo) {

		$strSubject = "問い合わせメールエラー";
		$strMailFrom = array(Apm_Config_Mail::MAIL_AS => "");		//送信元ﾒｰﾙｱﾄﾞﾚｽ
		$strMailTo = array(Apm_Config_Mail::MAIL_AS => "");			//送信先ﾒｰﾙｱﾄﾞﾚｽ

		//@ 送信先ﾒｰﾙｱﾄﾞﾚｽ
		// 本文の設定
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = $strSubject;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		if($addinfo['ComGenruFlg'] != "0") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($mail => "");
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailAsmemberNomail.tpl",array_merge($this->_params,$addinfo), $mail_params);
		} else {
			if($this->isSendHonbu() == true) {
				// ﾒｰﾙ送信処理
				$result = $this->sendToiawaseMail("MailAsmemberNomail.tpl",array_merge($this->_params,$addinfo), $mail_params);
			}else if($this->getReplaceHonbuMail() != "") {
				$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($this->getReplaceHonbuMail() => "");
				// ﾒｰﾙ送信処理
				$result = $this->sendToiawaseMail("MailAsmemberNomail.tpl",array_merge($this->_params,$addinfo), $mail_params);
			}
		}
		return $result;
	}
}
?>
